    /**
     * PrimeNmumbers
     * This program receives input of two strings and give the output to screen telling whether these words are anagrams
     * @author Maksym Ryumin
     */

    import java.util.*;
    import java.util.ArrayList;
    import static java.lang.Math.*;
    public class Anagram{

        public static void main(String[] args) {

            Scanner in = new Scanner(System.in);

            System.out.print("Please, enter first string \n");

            String string1 = in.nextLine();

            System.out.println("Now enter second string \n");
            String string2 = in.nextLine();

            in.close();

            string1 = string1.replace(" ", "").toLowerCase();
            string2 = string2.replace(" ", "").toLowerCase();

            ArrayList<String> stringArray1 = new ArrayList<String>(Arrays.asList(string1.split("")));
            ArrayList<String> stringArray2 = new ArrayList<String>(Arrays.asList(string2.split("")));

            boolean noMatch = false;
            int i=0;
            int j=0;

            no_match_exists:
            if ((noMatch == false) && (stringArray2.size() > 0)){
                if((stringArray1.size() == stringArray2.size())){
                    do{
                        match_exists:
                        for(j = 0; j < stringArray2.size(); j++){
                            if(stringArray1.get(i).equals(stringArray2.get(j))){
                                stringArray2.remove(j); i++;
                                break match_exists;
                            } else{
                                if(j == (stringArray2.size() - 1)){
                                    noMatch = true;
                                    break no_match_exists;
                                }
                            }
                        }
                    } while (i < stringArray1.size());
                }
                System.out.println("Entered words are anagrams!");

            } else{
            System.out.println("Entered words are not anagrams!");
              }
        }
    }
